
#include <cutils/memory.h>
#include <unistd.h>
// #include <utils/Log.h>
#include <binder/IPCThreadState.h>
#include <binder/ProcessState.h>
#include <binder/IServiceManager.h>
#include <gui/Surface.h>
#include <gui/SurfaceComposerClient.h>
#include <gui/ISurfaceComposer.h>
#include <ui/DisplayInfo.h>
#include <ui/Rect.h>
#include <ui/Region.h>
#include <android/native_window.h>
extern "C" {
#warning"i'm c++"
#include <png/png.h>
// #include <zlib.h>
}
// #include <SkGraphics.h>
// #include <SkBitmap.h>
// #include <SkCanvas.h>
// #include <SkDevice.h>
// #include <SkStream.h>
// #include <SkImageDecoder.h>
// #include <stdlib.h>
#include <hardware/hwcomposer_defs.h>
using namespace android;
#define IMG_PATH "system/bin/focus_calib.png"

int main(int argc, char** argv)
{

    int layer = 200000, status = 0;
    png_image image;
    png_bytep buffer;
    // image.version = PNG_VERSION;
    if (argc >= 3) {
        layer = strtol(argv[2], NULL, 10);
    }
    if (argc >= 2) {
        status = png_image_begin_read_from_file(&image, argv[1]);
    } else {
        status = png_image_begin_read_from_file(&image, IMG_PATH);
    }
    if (status == 0) {
          image.format = PNG_FORMAT_RGBA;
          int size = PNG_IMAGE_SIZE(image);
          buffer = (png_bytep)malloc(size);
          printf("img info:w=%d, h=%d, format=%d, size=%d\n",image.width, image.height, image.format, size);
    } else {
      printf("png err\n");
      return -1;
    }

    if (buffer != NULL && png_image_finish_read(&image, NULL/*background*/, buffer,0/*row_stride*/, NULL/*colormap*/) != 0) {
    }

    printf("show layer %d\n", layer);
    sp<ProcessState> proc = ProcessState::self();
    ProcessState::self()->startThreadPool();
    sp<SurfaceComposerClient> client = new SurfaceComposerClient();
    sp<IBinder> dtoken = SurfaceComposerClient::getBuiltInDisplay(ISurfaceComposer::eDisplayIdMain);
    DisplayInfo dinfo;
    status = SurfaceComposerClient::getDisplayInfo(dtoken, &dinfo);
    printf("display info:w=%d,h=%d,xdpi=%f,ydpi=%f,fps=%f,ds=%f\n", 
        dinfo.w, dinfo.h, dinfo.xdpi, dinfo.ydpi, dinfo.fps, dinfo.density);
    if (status)
        return -1;
    sp<SurfaceControl> surfaceControl = client->createSurface(String8("testsurface"),
            dinfo.w, dinfo.h, PIXEL_FORMAT_RGB_565, 0);
    SurfaceComposerClient::openGlobalTransaction();
    surfaceControl->setLayer(layer);//设定Z坐标
    surfaceControl->setPosition(0, 0);//以左上角为(0,0)设定显示位置
    SurfaceComposerClient::closeGlobalTransaction();
    surfaceControl->show();//感觉没有这步,图片也能显示
    sp<Surface> surface = surfaceControl->getSurface();
    ANativeWindow_Buffer outBuffer;
    //Surface::SurfaceoutBuffer outBuffer;
    surface->lock(&outBuffer, NULL);//获取surface缓冲区的地址
    ssize_t bpr = outBuffer.stride * bytesPerPixel(outBuffer.format);
    android_memset16((uint16_t*)outBuffer.bits, 0xF800, bpr*outBuffer.height);//往surface缓冲区塞要显示的RGB内容
    printf("buffer:size=%ld, w=%d, h=%d, stride=%d, bpr=%d\n", 
      (uint16_t*)outBuffer.bits, outBuffer.width, outBuffer.height, outBuffer.stride, bpr);
    surface->unlockAndPost();
    sleep(3);
    FILE *fp = fopen("system/bin/focus_calib.png","rb");//我们从一个文件里获取RGB565图像数据 480*272
    if(fp != NULL){
        unsigned char *rgb565Data = new unsigned char[480*272*2];
        memset(rgb565Data,0x00,480*272*2);
        fread(rgb565Data,1,480*272*2,fp);
        surface->lock(&outBuffer,NULL);
        memcpy(outBuffer.bits,rgb565Data,480*272*2);
        delete[] rgb565Data;
        surface->unlockAndPost();
    }
    fclose(fp);
    while(1) {
    }

    IPCThreadState::self()->joinThreadPool();
    IPCThreadState::self()->stopProcess();
    return 0;
}
